import json
import os
import sys
from graph import Graph

sys.setrecursionlimit(1000000)

if (len(sys.argv) < 2):
    print('''usage: python main.py <dataset>''')
    exit()

dataset = sys.argv[1]

# 读取文件中的 reads 片段
def get_reads(s):
    reads = []
    with open(s) as f:
        for line in f:
            if line[0] != '>':
                reads.append(line)
    return reads

# 读取数据集
def read_dataset(dataset):
    with open(os.path.join(dataset, 'param.json')) as f:
        params = json.load(f)
    reads = []
    short_fmt = 'short_{}.fasta'
    for i in range(1, 3):
        reads.extend(get_reads(os.path.join(dataset, short_fmt.format(i))))
    return reads, params
        
(reads, params) = read_dataset(dataset)

config = {
    'length': 25,
    'rev_comp': False
}
g = Graph(reads, config)
with open(os.path.join(dataset, 'mycontig.fasta'), 'w') as f:
    # while None != path and len(path) + 24 > params['short_read_length']:
    for i in range(20):
        print(i + 1)
        path = g.get_longest_path()
        s = g.path2str(path)
        if None == path:
            break
        # g.check()
        if None != s:
            f.write(s + '\n')
        g.remove_longest_path(path)

